
package com.zyh.service;

import com.zyh.dao.FundPayMapper;
import com.zyh.dao.TrandMapper;
import com.zyh.pojo.*;
import com.zyh.util.XstreamKit;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
public class FundPayResultServiceImpl implements FundPayResultService {


    @Autowired
    private TrandMapper trandMapper;
    @Autowired
    private FundPayMapper fundPayMapper;
    private static FundPayResultServiceImpl fundPayResultService;


    @Override
    public String queryResult(String xmlData) {
        //获取请求参数
        Map<String, String> mapr = doRequestObj(xmlData);
        String orderFlowNo = mapr.get("orderFlowNo");
        String reqTime = mapr.get("reqTime");
        String txnDt = reqTime.substring(0, 8);
        String txnTm = reqTime.substring(8, 14);
        OutFundPayOrderMessage ybxml = new OutFundPayOrderMessage();

        String responseToYBxml = null;
        String res = "";
        String status = "";
        String ret = "";
        //业务逻辑
        Map<String, Object> LogMap = fundPayMapper.queryTransactionStatus(orderFlowNo);
        if (LogMap != null && !LogMap.isEmpty()) {

            //返回医保端
            OutHeadPojo ybhead = new OutHeadPojo();
            ybhead.setIBSJnlNo(((String) LogMap.get("BsnSrlNo")).substring(14, 32));//网银流水号
            ybhead.setIBSReturnCode("000000");//网银返回码
            ybhead.setIBSReturnMsg("查询成功");
            ybhead.setIBSReturnMsg(ret);
            ybhead.setSerialNo((String) LogMap.get("SerialNo"));//交易流水号
            OutFundPayOrderPojo ybbody = new OutFundPayOrderPojo();
            ybbody.setProcstat((String) LogMap.get("Status"));
            ybbody.setOrderFlowDescript((String) LogMap.get("StatusMessage"));
            ybxml.setBody(ybbody);
            ybxml.setHead(ybhead);
            res = "基金支付结果查询成功";
            status = "90";
        } else {
            OutHeadPojo ybhead = new OutHeadPojo();
            ybhead.setIBSJnlNo("");//网银流水号
            ybhead.setIBSReturnCode("000000");//网银返回码
            ybhead.setIBSReturnMsg("查询成功");//网银返回信息
            ybhead.setSerialNo(orderFlowNo);//交易流水号
            ybxml.setHead(ybhead);
            OutFundPayOrderPojo ybbody = new OutFundPayOrderPojo();
            ybbody.setProcstat("91");
            ybbody.setOrderFlowDescript("未找到符合条件的记录！");
            ybxml.setBody(ybbody);
            res = "基金支付结果查询失败";
            status = "91";
        }
        //返回
        responseToYBxml = XstreamKit.vo2Xml(ybxml);
        //日志落库
        logFellLibrary(txnDt, txnTm, status, res, "", "", "", "", "", responseToYBxml, xmlData);

        return responseToYBxml;
    }

    //解析医保端请求方的XML数据
    private Map<String, String> doRequestObj(String xmlData) {
        Map<String, String> map = new HashMap<>();


        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xmlData);
            Element rootElt = doc.getRootElement();

            // 只返回 Head 内容
            Iterator itersHead = rootElt.elementIterator("Head");

            while (itersHead.hasNext()) {

                Element reEless = (Element) itersHead.next();
                String reqTime = reEless.elementTextTrim("reqTime");

                map.put("reqTime", reqTime);

            }

            // 只返回 Body 内容
            Iterator itersBody = rootElt.elementIterator("Body");
            while (itersBody.hasNext()) {

                Element reEless = (Element) itersBody.next();
                //账户流水查询用账户号【解析传入】
                String orderFlowNo = reEless.elementTextTrim("orderFlowNo");

                map.put("orderFlowNo", orderFlowNo);

            }


        } catch (DocumentException e) {
            System.out.println(e.getMessage());
        }

        return map;
    }


    /**
     * 日志落库
     */
    private void logFellLibrary(Object recDate, Object retTime, Object status, Object esbMsg, Object esbId, Object esbStatus, Object esbServiceCode, Object esbOut, Object esbIn, Object ybOut, Object ybIn) {
        Zjywpt_yg_trand_infos infos = new Zjywpt_yg_trand_infos();
        infos.setRECDATE((String) recDate);
        infos.setRETDATE((String) retTime);
        infos.setSTATUS((String) status);
        infos.setESBMSG((String) esbMsg);
        infos.setESBID((String) esbId);
        infos.setESBSTATUS((String) esbStatus);
        infos.setESBServiceCode((String) esbServiceCode);
        infos.setESBOUT((String) esbOut);
        infos.setESBIN((String) esbIn);
        infos.setYBOUT((String)ybOut);
        infos.setYBIN((String)ybIn);
        trandMapper.addTrand(infos);
    }


}

